package com.device.Controller;

import com.device.Service.FileUploadService;
import com.device.common.Result;

import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/files")
public class FileUploadController {
    @Resource
    private FileUploadService fileUploadService;
    @RequestMapping("/upload")
    public Result upload(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String url=fileUploadService.uploadFile(file,"cover");
        return Result.success( url);

    }
    @RequestMapping("/uploadeditor")
    public Result uploadeditor(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String url=fileUploadService.uploadFile(file,"cover");
      Map<String,Object> map=new HashMap<>();
      map.put("url",url);
        return Result.success(map);

    }
}
